package com.imageim.yimirror.common.ui.analysis;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.imageim.yimirror.R;
import com.imageim.yimirror.bean.analysisPhoto.AnalysisPhotoResultBean;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.animation.AnimatorSet;
import com.nineoldandroids.animation.ObjectAnimator;

import java.lang.ref.WeakReference;

/**
 * 服装分析view
 * Created by Bruce.Zhou on 2018/6/27 15:04.
 * Email: 907160968@qq.com
 */

public class ClothAnalysisView extends RelativeLayout {
    public ClothAnalysisView(Context context) {
        super(context);
        init();
    }

    public ClothAnalysisView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ClothAnalysisView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private View root;
    private View panelInfoView;
    private ImageView ivScanner;
    private static final int STATE_LOADING = 1;
    private static final int STATE_LOADED = 2;
    private int state = STATE_LOADING;

    private View panelArrow;
    private ImageView[] ivArrows = new ImageView[3];
    private static final int MSG_UPDATE_ARROW = 2;
    private int visiableNum = 1;
    // -------------------------------------
    private TextView[] tvTags = new TextView[16];
    private View line1;
    private View line2;
    private View line3;
    private View line4;

    private Handler handler;

    public static class MyHandler extends Handler {
        private WeakReference<ClothAnalysisView> reference;

        public MyHandler(ClothAnalysisView view) {
            reference = new WeakReference(view);
        }

        @Override
        public void handleMessage(Message msg) {
            ClothAnalysisView view = reference.get();
            if (view != null) {
                switch (msg.what) {
                    case MSG_UPDATE_ARROW:
                        view.visiableNum = view.visiableNum % 4;
                        for (int i = 0; i < 3; i++) {
                            if (i < view.visiableNum) {
                                view.ivArrows[i].setVisibility(View.VISIBLE);
                            } else {
                                view.ivArrows[i].setVisibility(View.INVISIBLE);
                            }
                        }
                        view.visiableNum++;
                        view.handler.removeMessages(MSG_UPDATE_ARROW);
                        sendEmptyMessageDelayed(MSG_UPDATE_ARROW, 490);
                        break;
                    default:
                        break;
                }
            }
        }
    }

    private void init() {
        handler = new MyHandler(this);
        root = View.inflate(getContext(), R.layout.layout_cloth_analysis_view, this);
        panelInfoView = root.findViewById(R.id.panel_info);
        ivScanner = root.findViewById(R.id.iv_scanner);
        panelArrow = root.findViewById(R.id.panel_arrow);
        ivArrows[0] = root.findViewById(R.id.iv_arrow1);
        ivArrows[1] = root.findViewById(R.id.iv_arrow2);
        ivArrows[2] = root.findViewById(R.id.iv_arrow3);
        panelArrow.setVisibility(View.INVISIBLE);
        ivArrows[0].setVisibility(View.INVISIBLE);
        ivArrows[1].setVisibility(View.INVISIBLE);
        ivArrows[2].setVisibility(View.INVISIBLE);
        // different views
        tvTags[0] = root.findViewById(R.id.tv_tag1);
        tvTags[1] = root.findViewById(R.id.tv_tag2);
        tvTags[2] = root.findViewById(R.id.tv_tag3);
        tvTags[3] = root.findViewById(R.id.tv_tag4);
        tvTags[4] = root.findViewById(R.id.tv_tag5);
        tvTags[5] = root.findViewById(R.id.tv_tag6);
        tvTags[6] = root.findViewById(R.id.tv_tag7);
        tvTags[7] = root.findViewById(R.id.tv_tag8);
        tvTags[8] = root.findViewById(R.id.tv_tag9);
        tvTags[9] = root.findViewById(R.id.tv_tag10);
        tvTags[10] = root.findViewById(R.id.tv_tag11);
        tvTags[11] = root.findViewById(R.id.tv_tag12);
        tvTags[12] = root.findViewById(R.id.tv_tag13);
        tvTags[13] = root.findViewById(R.id.tv_tag14);
        tvTags[14] = root.findViewById(R.id.tv_tag15);
        tvTags[15] = root.findViewById(R.id.tv_tag16);
        line1 = root.findViewById(R.id.line1);
        line2 = root.findViewById(R.id.line2);
        line3 = root.findViewById(R.id.line3);
        line4 = root.findViewById(R.id.line4);
        setState(STATE_LOADING);
        startLoadingAnim();
    }

    private void startLoadingAnim() {
        ObjectAnimator moveRight = ObjectAnimator.ofFloat(ivScanner, "translationX", 300 + 206, 0);
        moveRight.setDuration(2000);
        ivScanner.setRotation(180);
        moveRight.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // TODO 设置反向scanner
                ivScanner.setRotation(0);
            }
        });
        ObjectAnimator moveLeft = ObjectAnimator.ofFloat(ivScanner, "translationX", -206, 300);
        moveLeft.setDuration(2000);
        moveLeft.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // TODO 设置反向scanner
            }
        });
        AnimatorSet anim = new AnimatorSet();
        anim.playSequentially(moveRight, moveLeft);
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                if (state == STATE_LOADING) {
                    startLoadingAnim();
                } else {
                    ivScanner.setVisibility(View.GONE);
                }
            }
        });
        anim.start();
    }

    private void setState(int state) {
        if (state == STATE_LOADING) {
            panelInfoView.setVisibility(View.INVISIBLE);
            ivScanner.setVisibility(View.VISIBLE);
        } else if (state == STATE_LOADED) {
            panelInfoView.setVisibility(View.VISIBLE);
            ivScanner.setVisibility(View.INVISIBLE);
        }
    }

    public void setData(AnalysisPhotoResultBean data) {
        if (!TextUtils.isEmpty(data.getLabels())) {
            if (data.getIfAll() == 2) {
                line1.setVisibility(View.VISIBLE);
                line2.setVisibility(View.VISIBLE);
                line3.setVisibility(View.VISIBLE);
                line4.setVisibility(View.VISIBLE);
                for (int i = 0; i < tvTags.length; i++) {
                    tvTags[i].setVisibility(View.VISIBLE);
                }
                String[] tags = data.getLabels().split(",");
                int count = Math.min(tags.length, tvTags.length);
                for (int i = 0; i < count; i++) {
                    tvTags[i].setText(tags[i]);
                }
                for (int i = 0; i < tvTags.length - count; i++) {
                    tvTags[tvTags.length - i - 1].setVisibility(View.INVISIBLE);
                }
                int savedTagNum = tvTags.length - tags.length;
                if (savedTagNum >= 4) {
                    line4.setVisibility(View.GONE);
                }
                if (savedTagNum >= 8) {
                    line3.setVisibility(View.GONE);
                }
                if (savedTagNum >= 12) {
                    line2.setVisibility(View.GONE);
                }
                if (savedTagNum >= 16) {
                    line1.setVisibility(View.GONE);
                }
                setState(STATE_LOADED);
                startArrowAnim();
                startScaleAnim(true);
            }
        } else {
            if (data.getIfAll() == 2) {
                line2.setVisibility(View.GONE);
                line3.setVisibility(View.GONE);
                line4.setVisibility(View.GONE);
                for (int i = 1; i < 4; i++) {
                    tvTags[i].setVisibility(View.GONE);
                }
                tvTags[0].setText("无");
                setState(STATE_LOADED);
            }
        }
    }

    private void startArrowAnim() {
        panelArrow.setVisibility(View.VISIBLE);
        handler.sendEmptyMessage(MSG_UPDATE_ARROW);
    }

    private void startScaleAnim(boolean isFirst) {
        ObjectAnimator scaleX1 = ObjectAnimator.ofFloat(this, "scaleX", 0.97f);
        ObjectAnimator scaleY1 = ObjectAnimator.ofFloat(this, "scaleY", 0.97f);
        scaleX1.setDuration(2000);
        scaleY1.setDuration(2000);
        AnimatorSet scale1 = new AnimatorSet();
        scale1.playTogether(scaleX1, scaleY1);

        ObjectAnimator scaleX2 = ObjectAnimator.ofFloat(this, "scaleX", 1.03f);
        ObjectAnimator scaleY2 = ObjectAnimator.ofFloat(this, "scaleY", 1.03f);
        scaleX2.setDuration(2000);
        scaleY2.setDuration(2000);
        AnimatorSet scale2 = new AnimatorSet();
        scale2.playTogether(scaleX2, scaleY2);

        AnimatorSet scale = new AnimatorSet();
        scale.playSequentially(scale1, scale2);
        scale.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                startScaleAnim(false);
            }
        });
        if (isFirst) {
            scale.setStartDelay(300);
        }
        scale.start();
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        ivScanner.clearAnimation();
        clearAnimation();
        if (handler != null) {
            handler.removeCallbacksAndMessages(null);
        }
    }
}
